package com.bugbuff.dubbo.server.controller;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.common.utils.StringUtils;
import com.bugbuff.dubbo.server.service.TestSoulService;
import com.bugbuff.dubbo.server.vo.AnswerVo;
import com.bugbuff.dubbo.server.vo.QuestionnaireVo;
import com.bugbuff.dubbo.server.vo.ReportVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("testSoul")
@Slf4j
public class TestSoulController {

    @Autowired
    private TestSoulService testSoulService;

    /**
     * 测灵魂--问卷列表
     * @return
     */
    @GetMapping
    public ResponseEntity<List<QuestionnaireVo>> queryQuestionnaireList(){
        try {
            List<QuestionnaireVo> queryQuestionnaireList=testSoulService.queryQuestionnaireList();
            return ResponseEntity.ok(queryQuestionnaireList);
        } catch (Exception e) {
            log.error("查询问卷列表失败！");
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     *测灵魂-提交问卷
     * @param answers
     * @return
     */
    @PostMapping
    public ResponseEntity<String> submitQuestion(@RequestBody Map<String, List<AnswerVo>> answers){
        try {
            String reportId=testSoulService.submitQuestion(answers.get("answers"));
            if(StringUtils.isNotEmpty(reportId)){
                return ResponseEntity.ok(reportId);
            }
        } catch (Exception e) {
            log.error("问卷提交失败！");
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 查看测灵魂结果
     * @param reportId
     * @return
     */
    @GetMapping("/report/{id}")
    public ResponseEntity<ReportVo> queryReport(@PathVariable("id") String reportId){
        try {
            ReportVo reportVo=testSoulService.queryReport(reportId);
            if(ObjectUtil.isNotEmpty(reportVo)){
                return ResponseEntity.ok(reportVo);
            }
        } catch (Exception e) {
            log.error("查看结果失败！");
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
